package ch.mobi.osgi2maven2.script;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.context.Context;


public class ScriptGenerator {
	
	private static final String CONTEXT_NAME = "bundles";
	private static final String INSTALL_SCRIPT_TEMPLATE_NAME="install-script.vm";
	private static final String DEPLOY_SCRIPT_TEMPLATE_NAME="deploy-script.vm";
	
	public static void generateInstallScript(File file, List<JarPomPair> bundles) throws IOException {
		Context ctx=new VelocityContext();
		ctx.put(CONTEXT_NAME, bundles);
		FileWriter writer=new FileWriter(file);
		InputStream is=Thread.currentThread().getContextClassLoader().getResourceAsStream(INSTALL_SCRIPT_TEMPLATE_NAME);
		Velocity.evaluate(ctx, writer, INSTALL_SCRIPT_TEMPLATE_NAME, new InputStreamReader(is));
		writer.close();
		is.close();
	}

	public static void generateDeployScript(File file, List<JarPomPair> bundles) throws IOException {
		Context ctx=new VelocityContext();
		ctx.put(CONTEXT_NAME, bundles);
		FileWriter writer=new FileWriter(file);
		InputStream is=Thread.currentThread().getContextClassLoader().getResourceAsStream(DEPLOY_SCRIPT_TEMPLATE_NAME);
		Velocity.evaluate(ctx, writer, DEPLOY_SCRIPT_TEMPLATE_NAME, new InputStreamReader(is));
		writer.close();
		is.close();
	}

}
